package com.systar.activity.parse;

import org.junit.Test;

import com.castsoftware.java.FileExplorerMock.FileMock;
import com.google.common.base.Optional;
import com.systar.activity.monitor.ActivityManager;
import com.systar.activity.monitor.ActivitySource;
import com.systar.activity.state.FileActivity;

public class SessionFileParserRegExpTest extends ParseEnvironment
{
	private static final Optional<Integer> NONE = Optional.absent();
	
	private void runTest(final int expectedErrors, final Optional<Integer> readSize, final Optional<Integer> retentionSize) throws Exception
	{
		// configure virtual file
		final FileMock logFile = explorerMock.createFile(rootDir, "LogSession.xsession");
		logFile.addContent(getResourceContent("LogSession.xsession"));

		runTest(expectedErrors, new ParserBuilder()
		{
			@Override
			public FileParser buildParser(ActivityManager manager, ActivitySource source)
			{
				FileActivity activity = new FileActivity(source, logFile);
				SessionFileParserRegExp parser = new SessionFileParserRegExp(activity, source.getEventBus());
				if (readSize.isPresent())
					parser.setBuffers(readSize.get(), retentionSize.or(0));
				return parser;
			}

		});
	}

	/**
	 * Run parser with standard configuration 3 errors inside log file
	 * 
	 * @throws Exception
	 *             if any error
	 */
	@Test
	public void testStandard() throws Exception
	{
		runTest(3, NONE, NONE);
	}

	/**
	 * Run parser with read by blocks of 10K characters and no retention With
	 * this size, an error is split in 2 3 errors inside log file, but only 2
	 * are found in this configuration
	 * 
	 * @throws Exception
	 *             if any error
	 */
	@Test
	public void testNoRetention() throws Exception
	{
		runTest(2, Optional.of(10000), NONE);
	}

	/**
	 * Run parser with read by blocks of 10K characters with retention of 255
	 * chars With this size, an error is split in 2 3 errors inside log file,
	 * and all are found
	 * 
	 * @throws Exception
	 *             if any error
	 */
	@Test
	public void testWithRetention() throws Exception
	{
		runTest(3, Optional.of(10000), Optional.of(255));
	}

}
